#include "game.h"
void Initboard(char board[ROW][COL],int row,int col)
{
    int i = 0;
    int j = 0;
    for(i = 0;i < row;i++)
    {
        for(j = 0;j < col;j++)
        {
            board[i][j] = ' ';
        }
    }
}
void Displayboard(char board[ROW][COL],int row,int col)
{
    int i = 0;
    int j = 0;
    for(i = 0;i < row;i++)
    {
        for(j = 0;j < col;j++)
        {
            if(j < col - 1)
            {
                printf(" %c |",board[i][j]);
            }
            else
            {
                printf(" %c \n",board[i][j]);
            }
        }
        if(i < col - 1)
        {
            for (j = 0;j < col;j++)
            {
                printf("---");
                if(j < col - 1)
                printf("|");
            }
            printf("\n");
        }
    }
}
void Player_move(char board[ROW][COL],int row,int col)
{
    int a = 0;
    int b = 0;
    printf("玩家下棋\n");
    printf("请输入坐标；>\n");
    scanf("%d %d",&a,&b);
    while(1)
    if(a >= 1 && a <= row && b >= 1 && b <= col)
    {
        if(board[a-1][b-1] == ' ')
        {
            board[a-1][b-1] = '*';
            break;
        }
        else
        printf("坐标已被占用，请重新输入\n");
    }
    else
    {
        printf("坐标错误，请重新输入\n");
    }
}
void Computer_move(char board[ROW][COL],int row,int col)
{
    printf("电脑下棋\n");
    int a = 0;
    int b = 0;
    while(1)
    {
        int a = rand() % row;
        int b = rand() % col;   
        if(board[a][b] == ' ')
        {
            board[a][b]= '#';
            break;
        }
    }
}
int Is_full(char board[ROW][COL],int row,int col)
{ 
    int i = 0;
    int j = 0;
    for(i = 0; i < row;i++)
    {
        for(j = 0;j < col;j++)
        {
            if(board[i][j] == ' ')
            return 0;
        }
    }
    return 1;
}
char Is_win(char board[ROW][COL],int row,int col)
{
    //玩家赢了返回'*'
    //电脑赢了返回'#'
    //平局返回 'q'
    //未分胜负返回 'c'
    int i = 0;
    int j = 0;
    //行连线
    for(i = 0; i < row;i++)
    {
        if(board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
        {
            return board[i][1];
        }
    }
    //列连线
    for(j = 0; j < col;j++)
    {
        if(board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] != ' ')
        {
            return board[1][j];
        }
    }
    //对角线
    if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
    return board[1][1];
    if(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
    return board[1][1];
    //没人赢
    //棋盘满了，平局
    if(Is_full(board,row,col) == 1)
    {
        return 'q';
    }
    //棋盘未满，未分胜负，继续
    else
    {
        return 'c';
    }
}



